<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends CommonController {
    //显示商品列表
    public function index(){
        //查询所有商品
        $goods=M('Goods');
        //查询个数
        $count=$goods->count();
        //每页显示10个
        $page = new \Think\Page($count,6);
        //设置页码显示
        $page->setConfig('header', '共 %TOTAL_ROW% 条记录');
        $page->setConfig('first', '首页');
        $page->setConfig('last', '末页');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('theme',"共 %TOTAL_ROW% 条记录 %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $search=I('post.search');
        $a="";
        if($search){
            $this->assign('show', $a);
        }else{
            $this->assign('show', $page->show());
        }
        if($search){
            $cat=M('Cats');
            $data = $goods->order('timestamp desc')->page(I('get.p',1), 6)->where('goodsname="'.$search.'" OR goodscats="'.$search.'"')->join('yg_cats ON yg_goods.pcats = yg_cats.catsid')->select();
        }else{
            $cat=M('Cats');
            $data = $goods->order('timestamp desc')->page(I('get.p',1), 6)->join('yg_cats ON yg_goods.pcats = yg_cats.catsid')->select();

        }
        // 3.分配数据
        $this->assign('data', $data);
        //dump($data);
        $this->display();
    }


    //添加商品页面
    public function add(){
        //实例化分类模型
        $cats=D('Cats');
        //生成分级下拉列表
        $catsSelect=$cats->formSelect();
        //为模板变量赋值
        $this->assign('catsSelect',$catsSelect);
        //加载模板
        $this->display();
    }

    //添加商品数据操作
    public function insert(){
        //商品名为必填信息
        if(!$_POST['goodsname']){
            $this->error('请输入商品名','add',3);
            die();
        }
        // 实例化上传类
        $upload = new \Think\Upload();
        // 设置文件上传格式
        $upload->rootPath  = './Public/Upload/';
        $upload->savePath  = '';
        $upload->maxSize   =   2*1024*1024 ;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->saveName = time().'_'.mt_rand();
        //执行上传
        $result=$upload->upload();
        if(!$result){
            $this->error('上传文件失败，失败信息为:'.$upload->getError(),U('Goods/add'));
        }
        //dump($result);
        //接受上传图片信息
        $_POST['goodspic']=$result['goodspic']['savepath'].$result['goodspic']['savename'];
        //添加商品时间
        $_POST['addtime']=date('Y-m-d H:i:s',time());
        $_POST['timestamp']=time();
        //获取商品分类名
        $c=$_POST['goodscats'];
        dump($_POST);
        //实例化分类模型
        $cats=M('Cats');
        //查询商品二级分类的分类信息
        $catsall=$cats->where("catsname='".$c."'")->find();
        //获取顶级分类的id
        $gcats=$catsall['pid'];
        //获取父分类id
        $pcats=$catsall['catsid'];
        //把子父分类id存放在商品表
        $_POST['pcats']=$pcats;
        //把子分类的顶级分类的id存放在商品表
        $_POST['gcats']=$gcats;
        //执行添加
        $goods=M('goods');
        $goods->create();
        $res=$goods->add($_POST);
        if($res){
            $this->success('添加商品成功',U('Goods/index'));
        }else{
            $this->error('添加商品失败',U('Goods/add'));
        }

    }

    //修改商品页面
    public function mod(){
        //接受分类ID
        $goodsid=I('get.goodsid');
        // dump($goodsid);die();
        //查询原商品信息
        $goods=D('Goods');
        $info=$goods->where('goodsid='.$goodsid)->join('yg_cats ON yg_goods.pcats = yg_cats.catsid')->find();
        //分配到模板中
        // dump($info);die();
        $this->assign('goods',$info);

        $this->display();
    }

    //修改商品数据操作
    public function update(){
        if(!$_POST['goodsname']){
            $this->error('请输入商品名',U('Goods/mod','goodsid='.I('post.goodsid')));
            die();
        }
        $goods=M('Goods');
        //过滤非法信息
        $goods->create();
        //进行修改操作
        $res=$goods->save();
        if($res){
            $this->success('修改商品信息成功',U('Goods/index'));
        }else{
            $this->error('修改失败，请重新修改',U('Goods/mod','goodsid='.I('post.goodsid')));
        }
    }

    //删除商品操作
    public function delete(){
        //接收用户传过来的id进行删除操作
        $goodsid=I('get.goodsid');
        $goods=M('Goods');
        $res=$goods->delete($goodsid);
        if($res){
            //$this->success('删除商品成功',U('Goods/index'));
            $JSONObject['code'] =1;
            exit(json_encode($JSONObject));
        }else{
            //$this->error('删除商品失败',U('Goods/index'));
            $JSONObject['code'] =0;
            exit(json_encode($JSONObject));
        }
    }

    //查询商品数据操作
    public function modimg(){
        //接受分类ID
        $goodsid=I('get.goodsid');
        //查询原商品信息
        $goods=D('Goods');
        $info=$goods->where('goodsid='.$goodsid)->join('yg_cats ON yg_goods.pcats = yg_cats.catsid')->find();
        //分配到模板中
        $this->assign('goods',$info);
        $this->display();

    }

    public function imging(){

        $upload = new \Think\Upload();
        // 实例化上传类
        $upload->rootPath  = './Public/Upload/';
        $upload->savePath  = '';
        $upload->maxSize   =   2*1024*1024 ;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->saveName = time().'_'.mt_rand();
        // 设置附件上传大小
        $result=$upload->upload();

        if(!$result){
            $this->error('上传文件失败，失败信息为:'.$upload->getError(),U('Goods/modimg','goodsid='.I('post.goodsid')));
        }
        //接受上传信息
        $_POST['goodspic']=$result['goodspic']['savepath'].$result['goodspic']['savename'];

        //实例化goods表
        $goods=M('Goods');
        //过滤非法信息
        $goods->create();
        //进行修改操作
        $res=$goods->save();
        if($res){
            $this->success('修改商品信息成功',U('Goods/index'));
        }else{
            $this->error('修改失败，请重新修改',U('Goods/modimg','goodsid='.I('post.goodsid')));
        }

    }

}